<?php
/**
 * link: http://www.zjhejiang.com/
 * copyright: Copyright (c) 2018 浙江禾匠信息科技有限公司
 * author: wxf
 */

namespace app\plugins\scan_code_pay\forms\mall;

use app\core\response\ApiCode;
use app\models\Jiayou;
use app\models\Model;
use app\models\Store;

class JiayouEditForm extends Model
{

    public $id;
    public $qianghao;
    public $youxing;
    public $js_money;
    public $store_id;
    public $gz_id;

    public function rules()
    {
        return [
            [['store_id'], 'required'],
            [['qianghao', 'id', 'store_id','gz_id'], 'integer'],
            [['youxing', 'js_money'], 'string'],
        ];
    }

    public function save()
    {
        if (!$this->validate()) {
            return $this->getErrorResponse();
        }

        try {
            
            $store_data = Store::find()->where(['id'=>$this->store_id,'mall_id' => \Yii::$app->mall->id, 'is_delete' => 0])->one();

            if ($this->id) {
                $service = Jiayou::findOne(['mall_id' => \Yii::$app->mall->id, 'id' => $this->id]);

                if (!$service) {
                    return [
                        'code' => ApiCode::CODE_ERROR,
                        'msg' => '数据异常,该条数据不存在',
                    ];
                }

                if($service['store_id']!=$this->store_id || $service['qianghao']!=$this->qianghao){
                    $onlineInfo = Jiayou::findOne(['is_delete'=>0,'mall_id' => \Yii::$app->mall->id, 'store_id' => $this->store_id,'qianghao'=>$this->qianghao]);
                    if($onlineInfo->id){
                        return [
                            'code' => ApiCode::CODE_ERROR,
                            'msg' => $store_data['name'].'的'.$this->qianghao.'号油枪已添加，请勿重复添加！',
                        ];
                    }
                }


            } else {
                $service = new Jiayou();

                $service->mall_id = \Yii::$app->mall->id;

                $onlineInfo = Jiayou::findOne(['is_delete'=>0,'mall_id' => \Yii::$app->mall->id, 'store_id' => $this->store_id,'qianghao'=>$this->qianghao]);
                if($onlineInfo->id){
                    return [
                        'code' => ApiCode::CODE_ERROR,
                        'msg' => $store_data['name'].'的'.$this->qianghao.'号油枪已添加，请勿重复添加！',
                    ];
                }

            }

            $service->store_id = $this->store_id;
            $service->qianghao = $this->qianghao;
            $service->youxing = $this->youxing;
            $service->js_money = $this->js_money;
            $service->gz_id = $this->gz_id;
            $res = $service->save();

            if (!$res) {
                throw new \Exception($this->getErrorMsg($service));
            }

            return [
                'code' => ApiCode::CODE_SUCCESS,
                'msg' => '保存成功',
            ];
        } catch (\Exception $e) {
            return [
                'code' => ApiCode::CODE_ERROR,
                'msg' => $e->getMessage(),
            ];
        }
    }
}
